package com.zhongxi.guoyuan.adapter.seller;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.seller.SingleOrderInfo;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.view.rewardCoin.RewardCoinDetailActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/7/7.
 */

public class PostitemOrderListInfoAdapter extends BaseAdapter {

    private Context context;
    private List<SingleOrderInfo> orderInfoList;

    public PostitemOrderListInfoAdapter(Context context, List<SingleOrderInfo> orderInfoList) {
        this.context = context;
        this.orderInfoList = orderInfoList;
    }

    @Override
    public int getCount() {
        if (orderInfoList.size() > 0) {
            return orderInfoList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return orderInfoList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        try {
            ViewHolder vh;
            if (view == null) {
                vh = new ViewHolder();
                view = View.inflate(context, R.layout.item_order_list_seller, null);
                vh.ivImg = (ImageView) view.findViewById(R.id.ivImg);
                vh.tvName = (TextView) view.findViewById(R.id.tvName);
                vh.tvAttr = (TextView) view.findViewById(R.id.tvAttr);
                vh.tvPrice = (TextView) view.findViewById(R.id.tvPrice);
                vh.tvNum = (TextView) view.findViewById(R.id.tvNum);
                view.setTag(vh);
            } else {
                vh = (ViewHolder) view.getTag();
            }
            final SingleOrderInfo singleOrderInfo = orderInfoList.get(i);
            if (singleOrderInfo.getThumbnail() != null && !"".equals(singleOrderInfo.getThumbnail())) {
                Glide.with(context).load(Url.IMG_URL + singleOrderInfo.getThumbnail()).placeholder(R.mipmap.iv_eg).fitCenter().into(vh.ivImg);
            }
            if (singleOrderInfo.getGoodsname() != null && !"".equals(singleOrderInfo.getGoodsname())) {
                vh.tvName.setText(singleOrderInfo.getGoodsname());
            }
            if (singleOrderInfo.getGoodsunitprice() != null && !"".equals(singleOrderInfo.getGoodsunitprice())) {
                vh.tvPrice.setText(singleOrderInfo.getGoodsunitprice() + " 元");
            } else {
                vh.tvPrice.setText("0 元");
            }
            if (singleOrderInfo.getGattr() != null && !"".equals(singleOrderInfo.getGattr())) {
                String gattr = singleOrderInfo.getGattr();
                JSONObject jsonObject = new JSONObject(gattr);
                Iterator<String> keys = jsonObject.keys();
                String attr = "";
                while (keys.hasNext()) {
                    String next = keys.next();
                    String s = jsonObject.optString(next);
                    attr += s + "/";
                }
                vh.tvAttr.setText(attr.substring(0, attr.length() - 1));
            } else {
                vh.tvAttr.setText("");
            }
            if (singleOrderInfo.getGoodsnum() != null && !"".equals(singleOrderInfo.getGoodsnum())) {
                vh.tvNum.setText("x " + singleOrderInfo.getGoodsnum());
            } else {
                vh.tvNum.setText("x 0");
            }
//            view.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    Intent intent = new Intent(context, RewardCoinDetailActivity.class);
//                    intent.putExtra("gid", singleOrderInfo.getGoodsid());
//                    context.startActivity(intent);
//                }
//            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return view;
    }

    class ViewHolder {
        private ImageView ivImg;
        private TextView tvName;
        private TextView tvAttr;
        private TextView tvPrice;
        private TextView tvNum;
    }
}
